package com.rock.sys.config;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.eova.common.utils.xx;
import com.eova.config.EovaConfig;
import com.eova.interceptor.LoginInterceptor;
import com.eova.widget.upload.UploadAliyunController;
import com.eova.widget.upload.UploadController;
import com.eova.widget.upload.UploadQiniuController;
import com.jfinal.config.Constants;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.rock.cms.controller.RockCmsController;
import com.rock.cms.model.TApBanner;
import com.rock.cms.model.TCmArticle;
import com.rock.cms.model.TCmArticleCategory;
import com.rock.sys.controller.RockAppController;
import com.rock.sys.controller.RockChartController;
import com.rock.sys.controller.RockFormController;
import com.rock.sys.controller.RockOfficeControllerbak;
import com.rock.sys.controller.RockUserController;
import com.rock.sys.controller.RockWebPageController;
import com.rock.sys.controller.UploadRockLocalController;
import com.rock.sys.controller.UploadRockStaticController;
import com.rock.sys.global.RoleAppBindIntercept;
import com.rock.sys.hotdev.RockResourceLoader;
import com.rock.sys.model.TSApp;
import com.rock.sys.model.TSAppBindhis;
import com.rock.sys.model.TSAppRelease;
import com.rock.sys.model.TSAppRole;
import com.rock.sys.model.TSAppUser;
import com.rock.sys.model.TSComp;
import com.rock.sys.model.TSPage;
import com.rock.sys.model.TSPageComp;
import com.rock.sys.model.TSTenant;
import com.rock.sys.model.TSUserOpenid;
import com.rock.sys.model.TUser;


public class RockConfig extends EovaConfig {

	/** 所有模块配置类 **/
	public static List<RockMoudleConfig> allConfigs = new ArrayList<RockMoudleConfig>();
	
	/** 增加模块配置类 **/
	public static void addConfig(Class<? extends RockMoudleConfig> c) {
		try {
			allConfigs.add(c.newInstance());
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
	/**默认APP角色绑定拦截器**/
	private static RoleAppBindIntercept defaultRoleBindIntercept = null;
	
	
    /**
     * APP相关路由
     *
     * @param me
     */
    @Override
    protected void route(Routes me) {
    	//务必首先执行父类方法
    	super.route(me);
    	
    	//执行各子模块的方法
    	this.routeScan(me);
        
        //added by jadison 20200528 start
        //开放系统用户相关api能力
        me.add("/metapi/meta", RockUserController.class);
        LoginInterceptor.excludes.add("/metapi/**");
        
        //rock main首页
        me.add("/rock/page", RockWebPageController.class);
        me.add("/rock/chart", RockChartController.class);
        //rock app 导出配置相关
        me.add("/rock/app", RockAppController.class);
        //rock 通用文档查看相关
        me.add("/rock/form", RockFormController.class);
        
		//上传组件(有多种，本地、7牛 、阿里云、本地+静态站分离)
		if("0".equals(xx.getConfig("oss_static"))) 
			me.add("/upload", UploadRockLocalController.class);
		else if("1".equals(xx.getConfig("oss_static")))
			me.add("/upload", UploadQiniuController.class);
		else if("2".equals(xx.getConfig("oss_static")))
			me.add("/upload", UploadAliyunController.class);
		else if("3".equals(xx.getConfig("oss_static")))
			me.add("/upload", UploadRockStaticController.class);
		else
			me.add("/upload", UploadController.class);
		
		//微官网模块
        me.add("/rock/cms", RockCmsController.class);
        
    }

    /**
     * 自定义Main数据源Model映射
     *
     * @param arp
     */
    @Override
    protected void mapping(ActiveRecordPlugin arp) {
        super.mapping(arp);

    	//执行各子模块的方法
        this.mappingScan(arp);
        
        //APP相关
        arp.addMapping("t_s_app", TSApp.class);
        arp.addMapping("t_s_app_bindhis", TSAppBindhis.class);
        arp.addMapping("t_s_app_release", TSAppRelease.class);
        arp.addMapping("t_s_app_user", TSAppUser.class);
        arp.addMapping("t_s_comp", TSComp.class);
        arp.addMapping("t_s_page", TSPage.class);
        arp.addMapping("t_s_page_comp", TSPageComp.class);
        arp.addMapping("t_s_tenant", TSTenant.class);
        arp.addMapping("t_s_user_openid", TSUserOpenid.class);
        arp.addMapping("t_user", TUser.class);
        arp.addMapping("t_s_app_role", TSAppRole.class);
        
		//微官网模块
        arp.addMapping("t_cm_article", TCmArticle.class);
        arp.addMapping("t_cm_article_category", TCmArticleCategory.class);
        arp.addMapping("t_ap_banner", TApBanner.class);
    }

	@Override
    protected void auth() {
        super.auth();
    	//执行各子模块的方法
    	this.authScan(this.auths);
    	
        //此处设置需要用户登录的所有公共权限
        //将首页和图表设置为公共角色权限
        Set<String> uris = this.auths.get(0);
        uris.add("/rock/page/**");
        uris.add("/rock/chart/**");
        this.auths.put(0, uris);
    }
    
    /**
	 * 配置常量
	 */
	@Override
	public void configConstant(Constants me) {
		super.configConstant(me);
		
    	//开发模式下，启动静态资源实时监控
    	if(me.getDevMode()) {
    		new RockResourceLoader().start();
    	}
	}
	
    /**
     * 模块相关路由
     * @param me
     */
    protected void routeScan(Routes me) {
		System.out.println("-----------------遍历子模块路由-----------------");

		for(RockMoudleConfig moduleConfig : allConfigs) {
			moduleConfig.routeMoudle(me);
		}
    }

    /**
     * 模块相关权限
     * @param me
     */
    protected void authScan(Map<Integer, Set<String>> auths) {
		System.out.println("-----------------遍历子模块权限-----------------");
		for(RockMoudleConfig moduleConfig : allConfigs) {
			moduleConfig.authMoudle(auths);
		}
    }
    
    /**
     * 模块相关模型映射
     * @param arp
     */
    private void mappingScan(ActiveRecordPlugin arp) {
		System.out.println("-----------------遍历子模块模型映射-----------------");
		for(RockMoudleConfig moduleConfig : allConfigs) {
			moduleConfig.mappingMoudle(arp);
		}
    }
    
	public static RoleAppBindIntercept getDefaultRoleBindIntercept() {
		return defaultRoleBindIntercept;
	}

	public static void setDefaultRoleBindIntercept(RoleAppBindIntercept defaultRoleBindIntercept) {
		RockConfig.defaultRoleBindIntercept = defaultRoleBindIntercept;
	}
	
	/** 初始化所有配置类 **/
//	public static void initAllConfigs() {
//        Set<Class<?>> modelClassSet = ClassScaner.scanPackageByAnnotation(null, RockConfig.class);
//        if (ArrayUtil.isAllEmpty(modelClassSet)) {
//            return;
//        }
//        
//        for (Class<?> clazz : modelClassSet) {
//            RockConfig tb = clazz.getAnnotation(RockConfig.class);
//            if (tb == null) {
//                continue;
//            }
//            System.out.println("加载类名：" + clazz.getName());
//        }
//	}

}